/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Calendar;

/**
 *
 * @author Administrator
 */
public class YearoutDao {
    private Connection conn = null;
    public int size;

    public YearoutDao(Connection conn) {
        this.conn = conn;
    }


    public boolean doCreate(YearoutBean yearoutBean) throws Exception {
            boolean flag = false;
            PreparedStatement pstmt = null;
             String sql = "INSERT INTO [yearout]([orgcode],[year],[yearout],[status])"
                        + " VALUES (?,?,?,?)";
        try {
                pstmt = this.conn.prepareStatement(sql);

                pstmt.setString(1, yearoutBean.getMineCode());			
                pstmt.setInt(2, yearoutBean.getYear());                              
                pstmt.setDouble(3, yearoutBean.getYearout());
                pstmt.setByte(4, (byte)1);

                int len = pstmt.executeUpdate();
                if (len > 0) {
                        flag = true;
                }
        } catch (Exception e) {
                throw e;
        } finally {
                try {
                        pstmt.close();
                } catch (Exception e) {
                        throw e;
                }
        }
        return flag;
    }
   
   /**
     * 创建一个新记录，为新数据存储作准备
     * @param orgCode
     * @param year
     * @return
     * @throws Exception 
     */ 
   public boolean doCreate(String orgCode,int year) throws Exception {
            boolean flag = false;
            PreparedStatement pstmt = null;
             String sql = "INSERT INTO [yearout]([orgcode],[year],[yearout],[status])"
                        + " VALUES (?,?,?,?)";
        try {
                pstmt = this.conn.prepareStatement(sql);

                pstmt.setString(1, orgCode);			
                pstmt.setInt(2, year);     
                pstmt.setDouble(3, 0);
                pstmt.setByte(4, (byte)0);
                int len = pstmt.executeUpdate();
                if (len > 0) {
                        flag = true;
                }
        } catch (Exception e) {
                throw e;
        } finally {
                try {
                        pstmt.close();
                } catch (Exception e) {
                        throw e;
                }
        }
        return flag;
    }
   
   public boolean addOutput( DataOf5mBean d5m) throws Exception {
            boolean flag = false;
            PreparedStatement pstmt = null;
            String orgCode = d5m.getMineCode();
            int year = d5m.getYear();
            
            
            String sql = "UPDATE [yearout] SET [yearout]=[yearout]+?,[status]=? WHERE [orgcode]=? and [year]=? ";
            try {
                    pstmt = this.conn.prepareStatement(sql);
                    
                    pstmt.setDouble(1, d5m.getSumof5min());
                    pstmt.setByte(2, (byte)1);     
                    pstmt.setString(3, orgCode);			
                    pstmt.setInt(4, year);
                       
                    int len = pstmt.executeUpdate();
                    if (len > 0) {
                            flag = true;
                    }
            } catch (Exception e) {
                    throw e;
            } finally {
                    try {
                            pstmt.close();
                    } catch (Exception e) {
                            throw e;
                    }
            }
            return flag;
    }
    
    public boolean doUpdateOut(DataOf5mBean d5m) throws Exception {
            boolean flag = false;
            boolean flagCreate=false;
            boolean flagAdd =false;
            
            String orgCode = d5m.getMineCode();
            int year = d5m.getYear();
            
            if(!isExist(orgCode,year)){
                flagCreate=doCreate(orgCode,year);
            }
            flagAdd = addOutput(d5m);
            flag = flagCreate&&flagAdd;
            return flag;
            
    }
   
   
    //*更新产量，进行累加
//     public boolean doUpdateOut(DataOf5mBean d5m) throws Exception {
//            boolean flag = false;
//            PreparedStatement pstmt = null;
//            
//            Calendar calendar = Calendar.getInstance();
//            String orgCode = d5m.getMineCode();
//            int year = d5m.getYear();
//            if(d5m.getMin()==0||d5m.getMin()==1){     //整点数据归为上个小时
//                calendar.set(d5m.getYear(), d5m.getMonth()-1, d5m.getDay(), d5m.getHour(), d5m.getMin());
//                calendar.add(Calendar.MINUTE, -2);
//                 year = calendar.get(Calendar.YEAR);
//            }
//            
//            if(!isExist(orgCode,year)){
//                doCreate(orgCode,year);
//            }            
//            String sql = "UPDATE [yearout] SET [yearout]=[yearout]+?,[status]=? WHERE [orgcode]=? and [year]=? ";
//            try {
//                    pstmt = this.conn.prepareStatement(sql);
//
//                    pstmt.setString(3,orgCode);
//                    pstmt.setInt(4, year);
//                                              
//                    
//                    pstmt.setDouble(1, d5m.getSumof5min());
//                    pstmt.setByte(2, (byte)1);                    
//                    
//
//                    int len = pstmt.executeUpdate();
//                    if (len > 0) {
//                            flag = true;
//                    }
//            } catch (Exception e) {
//                    throw e;
//            } finally {
//                    try {
//                            pstmt.close();
//                    } catch (Exception e) {
//                            throw e;
//                    }
//            }
//            return flag;
//    }
   
    public boolean doDeleteByOrgTime(String orgCode,int year) throws Exception {
            boolean flag = false;
            PreparedStatement pstmt = null;
            String sql = "DELETE FROM yearout WHERE   orgCode=? and year=?";
            try {
                    pstmt = this.conn.prepareStatement(sql);
                    pstmt.setString(1, orgCode);
                    pstmt.setInt(2, year);                    
                    
                    int len = pstmt.executeUpdate();
                    if (len > 0) {
                            flag = true;
                    }
            } catch (Exception e) {
                    throw e;
            } finally {
                    try {
                            pstmt.close();
                    } catch (Exception e) {
                            throw e;
                    }
            }
            return flag;
    }
    public boolean doDeleteById(long pid) throws Exception {
            boolean flag = false;
            PreparedStatement pstmt = null;
            String sql = "DELETE FROM hourout WHERE id=?";
            try {
                    pstmt = this.conn.prepareStatement(sql);
                    pstmt.setLong(1, pid);
                    int len = pstmt.executeUpdate();
                    if (len > 0) {
                            flag = true;
                    }
            } catch (Exception e) {
                    throw e;
            } finally {
                    try {
                            pstmt.close();
                    } catch (Exception e) {
                            throw e;
                    }
            }
            return flag;
    }

    public boolean doDeleteById(String[] pids) throws Exception {
            for (String pid : pids) {
                long id = Long.parseLong(pid);
                doDeleteById(id);
            }
            return true;
    }
    
    public boolean isExist(String orgCode,int year) throws Exception {
        
            PreparedStatement pstmt = null;
            String sql = "select * FROM yearout WHERE   orgCode=? and year=?";
            try {
                    pstmt = this.conn.prepareStatement(sql);
                    pstmt.setString(1, orgCode);
                    pstmt.setInt(2, year);                   
                    
                    ResultSet rs = pstmt.executeQuery();
                    if(rs.next())
                        return true;
                    else
                        return false;
                    
            } catch (Exception e) {
                    throw e;
            } finally {
                    try {
                            pstmt.close();
                    } catch (Exception e) {
                            throw e;
                    }
            }          
    }
    
    public int getTotalSize() {
            return size;
    }
}
